// pages/addplan/addplan.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        colors:[{color:'rgb(200,255,230)',ischecked:false},
        {color:'rgb(255,196,34)',ischecked:false},
        {color:'rgb(200,220,230)',ischecked:false},
        {color:'rgb(100,255,150)',ischecked:false},
        {color:'rgb(255,220,100)',ischecked:false},],
        day:7,
        second:1,
        title:''
    },
    chooseColor:function(res){
        console.log(res)
        var index = res.currentTarget.dataset.index
        for(var i = 0;i<this.data.colors.length;i++){
            var s = 'colors['+i+'].ischecked'
            this.setData({
                [s]:false
            })
        }
        var s = 'colors['+index+'].ischecked'
        this.setData({
            [s] : true
        })
    },
    jianDay:function(){
        var day = this.data.day-1
        if (day===0){
            wx.showToast({
              title: '已经最少啦~',
              icon:'none'
            })
            return
        }
        this.setData({
            day:day
        })
    },
    getTitle:function(res){
        var title = res.detail.value;
        this.setData({
            title:title
        })
    },
    addDay:function(){
        var day = this.data.day+1
        this.setData({
            day:day
        })
    },
    jianSec:function(){
        var second = this.data.second-1
        if (second===0){
            wx.showToast({
              title: '已经最少啦~',
              icon:'none'
            })
            return
        }
        this.setData({
            second:second
        })
    },
    addSec:function(){
        var second = this.data.second+1
        this.setData({
            second:second
        })
    },
    submit:function(){
        console.log('点击了')
        var color = '';
        console.log(111)
        for(var i = 0;i<this.data.colors.length;i++){
            if(this.data.colors[i].ischecked === true)
            {
                color = this.data.colors[i].color;
                break;
            }
            else{
                continue;
            }
        }
        console.log(222)
        var days;
        days=this.data.day;
        var Sec;
        Sec = this.data.second;
        var title = this.data.title;
        if(color === ''){
            wx.showToast({
              title: '请选择颜色',
              icon:'none'
            })
        }
        if(color ==='' || days==='' || title==='' || Sec===''){
            wx.showToast({
              title: '没填完!😅',
              icon:'none'
            })
        }
        else{
            wx.request({
                url: 'http://127.0.0.1:8000/addPlan/',
                method:'POST',
                data:{
                    color:color,
                    days:days,
                    sec:Sec,
                    title:title,
                    openid:app.globalData.openid
                },
                success:function(res){
                  // 返回首页
                  wx.navigateBack({
                      delta: 0,
                  })
                }
              })
        }
        console.log('done')
        // 提交到django
        
    },
    cancel:function(){
        wx.navigateBack({
          delta: 0,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})